<template>
	<view class="page">
		<view class="info_box" style="padding-bottom: 40rpx;">
			<view class="info_box_base">
				<view class="info_box_base_title" style="font-weight: bold;">室外接入信号测试</view>

				<view class="info_box_base_item_1">
					<view class="info_box_base_item_img_title">5G测试照片</view>
					<view class="info_box_base_item_img_box">
						<u-upload :fileList="fileList1" width="65" height="65" @afterRead="afterRead"
							@delete="deletePic" name="1" multiple :maxCount="10"></u-upload>
					</view>
				</view>

				<view class="info_box_base_item_1">
					<view class="info_box_base_item_img_title">4G测试照片</view>
					<view class="info_box_base_item_img_box">
						<u-upload :fileList="fileList2" width="65" height="65" @afterRead="afterRead"
							@delete="deletePic" name="2" multiple :maxCount="10"></u-upload>
					</view>
				</view>

			</view>

			<view class="info_box_base">

				<view class="info_box_base_title" style="font-weight: bold;margin: 20rpx 0;">覆盖设备</view>
				<view class="info_box_base_btns">
					<view v-for="(item,index) in coverageofdevice_testingdata">
						<!-- <view @click="chooseSb(item)" class="info_box_base_btns_item info_box_base_btns_item_active"
							v-if="isInArray(item.id, currentSbids)">{{item.title}}</view> -->
						<view @click="chooseSb(item, index)" :class="item.is_full?'':'info_box_base_btns_item_active'" class="info_box_base_btns_item">{{item.number_code}}号设备
						</view>
					</view>

				</view>



				<view class="info_box_base_item_1">
					<view class="info_box_base_item_img_title"><span style="color: red;">*</span>5G测试照片</view>
					<view class="info_box_base_item_img_box">
						<u-upload :fileList="fileList3" width="65" height="65" @afterRead="afterRead"
							@delete="deletePic" name="3" multiple :maxCount="10"></u-upload>
					</view>
				</view>

				<view class="info_box_base_item_1">
					<view class="info_box_base_item_img_title"><span style="color: red;">*</span>4G测试照片</view>
					<view class="info_box_base_item_img_box">
						<u-upload :fileList="fileList4" width="65" height="65" @afterRead="afterRead"
							@delete="deletePic" name="4" multiple :maxCount="10"></u-upload>
					</view>
				</view>
			</view>


			<view class="btn" @click="next">下一步</view>
		</view>
	</view>
</template>

<script>
	import {
		api,
		apiBaseUrl,
		http
	} from '@/config/common.js'
	export default {
		data() {
			return {
				id: '',
				packid: '',
				site_id: '',
				fgsbdata: [],
				fileList1: [],
				fileList2: [],
				fileList3: [],
				fileList4: [],
				currentSbids: [],
				currentIndex:0,
				coverageofdevice_testingdata: [],
				params: {

				}
			}
		},
		components: {},
		filters: {},
		mounted() {},
		onLoad(e) {
			this.id = e.id
			this.packid = e.packid
			this.site_id = e.site_id
			this.lookInfo()
			// this.getfgsb()
		},
		onShow() {},
		onPageScroll(e) {},
		beforeDestroy() {},
		methods: {
			chooseSb(item, index) {
				this.currentIndex = index
				let temp_3 = []
				item.coveragedevice_5gimg = item.coveragedevice_5gimg ? item.coveragedevice_5gimg : []
				item.coveragedevice_5gimg.forEach((item, index) => {
					if (item.url) {
						temp_3.push({
							url: item.url
						})
					} else {
						temp_3.push({
							url: item
						})
					}
				})
				this.fileList3 = temp_3
				let temp_4 = []
				item.coveragedevice_4gimg = item.coveragedevice_4gimg ? item.coveragedevice_4gimg : []
				item.coveragedevice_4gimg.forEach((item, index) => {
					if (item.url) {
						temp_4.push({
							url: item.url
						})
					} else {
						temp_4.push({
							url: item
						})
					}
				})
				this.fileList4 = temp_4 
			},
			// isInArray(item, arr) {
			// 	return arr.includes(item);
			// },
			//覆盖设备
			// getfgsb() {
			// 	uni.showLoading({})
			// 	api.post('/api/shop/engin_coverage_device', {}).then(res => {
			// 		let rst = res.data
			// 		uni.hideLoading()
			// 		if (rst.code == 1) {
			// 			this.fgsbdata = rst.data
			// 		}
			// 	}).catch(err => {
			// 		uni.hideLoading()
			// 	})
			// },
			//检查是否存在输入数据
			checkTestData(){
				let rst = this.coverageofdevice_testingdata 
				rst.forEach((item, index)=>{   
					 
					let tcoveragedevice_5gimg = item.coveragedevice_5gimg? item.coveragedevice_5gimg.join(','):''
					let tcoveragedevice_4gimg = item.coveragedevice_4gimg? item.coveragedevice_4gimg.join(','):''
					 
					if (tcoveragedevice_5gimg && tcoveragedevice_4gimg) {
						this.coverageofdevice_testingdata[index]['is_full'] = true; 
					} else {
						this.coverageofdevice_testingdata[index]['is_full'] = false; 
					}
				}) 
			},
			next() { 
				let rst = this.params 
				rst.outdoor_5gimg = this.fileList1 ? this.fileList1.map(item => item.url).join(',') : [];
				rst.outdoor_4gimg = this.fileList2 ? this.fileList2.map(item => item.url).join(',') : []; 
				
				let rst_test_img = this.coverageofdevice_testingdata ? this.coverageofdevice_testingdata : []
				let issb = true
				rst_test_img.forEach(function(item){
					if (!item.is_full) {
						 issb = false
					}
				})
				if (!issb) {
					uni.$u.toast("请完善覆盖设备信息")
					return
				}
				
				rst_test_img = rst_test_img.map(({ id,is_full, ...rest }) => rest)
				
				rst_test_img.forEach(function(itemrst, index) {
					rst_test_img[index].coveragedevice_5gimg = itemrst.coveragedevice_5gimg ? itemrst
						.coveragedevice_5gimg.map(function(item){
							if (item.url){
								return item.url
							} else {
								return item
							}
						}).join(',') : '';
					rst_test_img[index].coveragedevice_4gimg = itemrst.coveragedevice_4gimg ? itemrst
						.coveragedevice_4gimg.map(function(item){
							if (item.url){
								return item.url
							} else {
								return item
							}
						}).join(',') : '';
				});
				rst.coverageofdevice_testingdata = rst_test_img
				

				// rst.coverage_device_id = this.currentSbids.join(',')

				uni.showLoading({})
				api.post('api/shop/submit_onsitenetwork_testinge', {
					onsite_id: this.id,
					work_package_id:this.packid,
					work_site:this.site_id,
					...rst,
					type: 1
				}).then(res => {
					let rst = res.data
					
					uni.hideLoading()
					if (rst.code == 1) {
						uni.navigateTo({
							url: '/pages/xj/xjjgz?id='+this.id+'&packid='+this.packid+'&site_id='+this.site_id
						})
					} else {
						uni.$u.toast(rst.msg)
					}

				}).catch(err => {
					uni.hideLoading()
				})

			},
			//查找录入信息
			lookInfo() {
				uni.showLoading({})
				api.post('/api/shop/submit_onsitenetwork_testinge', {
					onsite_id: this.id,
					work_package_id: this.packid,
					work_site: this.site_id,
					type: 2
				}).then(res => {
					if (!res.data) {
						return
					}
					let rst = res.data
					uni.hideLoading()
					if (rst.code == 1) { 
						if (rst.data){ 
							this.params = rst.data 
							 
							if (rst.data.coverageofdevice_testingdata.length > 0) {
								this.chooseSb(rst.data.coverageofdevice_testingdata[0], 0)
							}  
							this.coverageofdevice_testingdata = rst.data.coverageofdevice_testingdata
							
							// this.currentSbids = rst.data.coverage_device_id ? rst.data.coverage_device_id.map(item => parseInt(item,10)):[]
							
							let temp_1 = []
							rst.data.outdoor_5gimg = rst.data.outdoor_5gimg ? rst.data.outdoor_5gimg : []
							rst.data.outdoor_5gimg.forEach((item, index) => {
								temp_1.push({
									url: item
								})
							}) 
							this.fileList1 = temp_1
							
							let temp_2 = []
							rst.data.outdoor_4gimg = rst.data.outdoor_4gimg ? rst.data.outdoor_4gimg : []
							rst.data.outdoor_4gimg.forEach((item, index) => {
								temp_2.push({
									url: item
								})
							})
							this.fileList2 = temp_2
							this.checkTestData();
							// let temp_3 = []
							// rst.data.coveragedevice_5gimg = rst.data.coveragedevice_5gimg ? rst.data
							// 	.coveragedevice_5gimg : []
							// rst.data.coveragedevice_5gimg.forEach((item, index) => {
							// 	temp_3.push({
							// 		url: item
							// 	})
							// })
							// this.fileList3 = temp_3
							
							// let temp_4 = []
							// rst.data.coveragedevice_4gimg = rst.data.coveragedevice_4gimg ? rst.data
							// 	.coveragedevice_4gimg : []
							// rst.data.coveragedevice_4gimg.forEach((item, index) => {
							// 	temp_4.push({
							// 		url: item
							// 	})
							// })
							// this.fileList4 = temp_4
						}
						
					}

				}).catch(err => {
					uni.hideLoading()
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				
				if (event.name == 3) {
					this.coverageofdevice_testingdata[this.currentIndex].coveragedevice_5gimg = this[`fileList${event.name}`] 
					this.checkTestData()
				}
				if (event.name == 4) {
					this.coverageofdevice_testingdata[this.currentIndex].coveragedevice_4gimg = this[`fileList${event.name}`] 
					this.checkTestData()
				}
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					
					if (event.name == 3) {
						this.coverageofdevice_testingdata[this.currentIndex].coveragedevice_5gimg = this[`fileList${event.name}`]
						this.checkTestData()
					}
					if (event.name == 4) {
						this.coverageofdevice_testingdata[this.currentIndex].coveragedevice_4gimg = this[`fileList${event.name}`]
						this.checkTestData()
					}
					
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: apiBaseUrl + '/api/shop/upload',
						filePath: url,
						name: 'file',
						header: {
							'method': "xj_user",
							'token': uni.getStorageSync('token')
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								let rst = JSON.parse(res.data)
								if (rst.code == 1) {
									resolve(rst.data)
								} else {
									uni.$u.toast(rst.msg)
									this.deletePic(event)
								}
							}, 300)
						}
					});
				})
			},
		}
	};
</script>

<style scoped>
	.info_box_base_btns_item_active {
		background-color: rgb(242, 64, 64) !important;
	}

	.info_box_base_btns_item {
		font-size: 25rpx;
		text-align: center;
		color: rgb(51, 51, 51);
		white-space: pre-wrap;
		word-break: break-word;
		max-width: 148rpx;
		background-color: rgb(17, 200, 97);
		padding: 10rpx 20rpx;
		border-radius: 10rpx;
		margin-right: 40rpx;
		margin-top: 20rpx;
	}

	.info_box_base_btns {
		display: flex;
		flex-direction: row;
		align-items: center;
		flex-wrap: wrap;
	}

	.btn {
		width: 500rpx;
		height: 80rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		margin: 0 auto;
		margin-top: 40rpx;
		background-color: rgb(70, 179, 255);
		color: #fff;
		border-radius: 40rpx;
		font-size: 32rpx;
	}

	::v-deep .uni-input-input {
		font-size: 26rpx;
	}

	::v-deep .uni-input-placeholder {
		font-size: 26rpx;
	}

	.info_box_base_item_img_title {
		font-size: 25rpx;
		margin: 20rpx 0;
	}

	.gcxt_b_wy_reason {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.gcxt_b_wy {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin: 20rpx 0;
	}

	.info_box_base {
		border: 1px solid rgb(235, 229, 229);
		padding: 20rpx;
		font-size: 25rpx;
		margin-top: 20rpx;
	}

	.info_box_base_item {
		line-height: 45rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.info_box {
		width: 710rpx;
		margin: 0 auto;

	}

	page {
		background-color: #fff;
	}
</style>